#导入显示等待
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select                #下拉框
from selenium.webdriver.common.keys import Keys                     #键盘事件
from selenium.webdriver.common.action_chains import ActionChains    #鼠标事件
#定义基类
class Base():
    #初始化方法
    def __init__(self,driver):
        self.driver = driver
    #浏览器方法
    def goto(self,url):
        self.driver.get(url)
    #定位元素方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(
                lambda driver: self.driver.find_element(*loc).is_displayed
            )
            return self.driver.find_element(*loc)
        except Exception as e:
            #定义时间戳
            t = time.strftime('%Y%m%d_%H%M%S')
            #异常截图+时间戳
            self.driver.save_screenshot(t+'baidu.png')
            print(e)
    #点击事件方法
    def click(self,loc):
        self.find_element(loc).click()
    #输入事件方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    #文本事件方法
    def text(self,loc):
        return self.find_element(loc).text
    #清空事件方法
    def clear(self,loc):
        self.find_element(loc).clear()

    #五种特殊处理
    #切换窗口
    def new_window(self):
        self.driver.switch_to.new_window(self.driver.window_handles[-1])
    #frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #滚动到任意位置
    def scroll_to_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    #滚动到底部
    def scrollto_Height(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

    #alert弹框——accept
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accept()
    #alert弹框——dismiss
    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    #下拉框:
    #下拉框——value
    def Select_value(self,loc,info):
        targe = Select(self.find_element(loc))
        targe.select_by_value(info)

    # 下拉框——index
    def Select_index(self,loc,x):
        targe = Select(self.find_element(loc))
        targe.select_by_index(x)
    #下拉框——visible_text
    def Select_visible_text(self,loc,info):
        targe = Select(self.find_element(loc))
        targe.select_by_visible_text(info)
    #键盘事件
    #全选
    def Ctrl_a(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')
    #复制
    def Ctrl_c(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    #粘贴
    def Ctrl_v(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')
    #剪切
    def Ctrl_x(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'x')
    #Tab键
    def Tab(self,loc):
        self.find_element(loc).send_keys(Keys.TAB)
    #空格键
    def Space(self,loc):
        self.find_element(loc).send_keys(Keys.SPACE)

    #鼠标事件:
    #双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #三击
    def Three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()
    #悬空
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    #按住不放
    def click_and_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()
    #右击
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()

    #浏览器事件:
    #刷新事件
    def refresh(self):
        self.driver.refresh()

    #放大屏幕
    def maximize_window(self):
        self.driver.maximize_window()

    #退出
    def quite(self):
        self.driver.quit()










